package modules.majiang.effect
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import modules.GameDispatcher;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;
	import modules.manager.SoundManager;
	
	import utils.BigCardFactory;

	/**
	 * 出牌特效数据
	 * @author xumin.xu
	 */
	public class DiscardEffectData
	{

		public var tableDataModel:TableDataModel;

		public function DiscardEffectData(tableDataModel:TableDataModel)
		{
			this.tableDataModel = tableDataModel;
		}

		private function get dispatcher():GameDispatcher
		{
			return GameDispatcher.instance;
		}

		/** 飞牌前显示的牌的位置 */
		private var _showCardsPostion:Array;

		private function get showCardsPostion():Array
		{
			if (_showCardsPostion == null)
			{
				_showCardsPostion = [];
				_showCardsPostion[1] = new Point(464, 450);
				_showCardsPostion[2] = new Point(464 - 247, 325 - 23);
				_showCardsPostion[3] = new Point(464 - 236, 325 - 118);
				_showCardsPostion[4] = new Point(464 + 16, 325 - 175);
				_showCardsPostion[5] = new Point(464 + 285, 325 - 133);
				_showCardsPostion[6] = new Point(464 + 308, 325 - 11);
			}
			return _showCardsPostion;
		}

		/**
		 * 播放飞牌特效
		 * @param discards 飞出去的牌信息
		 * @param ownCardIndex 自己出牌的位置
		 */
		public function discardCard(discards:Array, ownCardIndex:int):void
		{
			var i:int;
			for (i = 0; i < discards.length; i++)
			{
				var discard:Object = discards[i];

				var chairId:int = discard.chairId;

				var showId:int = tableDataModel.getPositionByChairId(chairId);

				var index:int = discard.onTableIndex;
				
				var position:int = tableDataModel.getPositionByChairId(chairId);

				var data:Object = {position: position, index: index};
				//获取牌
				dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.GET_DISCARD, data));
				//获取桌面上的牌
				dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.GET_CARDPOT_CARD, data));
				var targetCard:MovieClip = data.targetCard;
				var showCard:MovieClip = data.card;

				var showCardPosition:Point = showCardsPostion[showId];
				if (showCard)
				{
					showCard.width = 51;
					showCard.height = 67;
					var cardBitmap:Bitmap = showCard.getChildByName("cardBitmap") as Bitmap;
					if (cardBitmap)
					{
						cardBitmap.bitmapData = BigCardFactory.getNormalCardBitmapData(discard.type, discard.value);
						cardBitmap.visible = true;
					}
				}

				var duration:Number = 0.7;
				//如果显示自己出牌时
				if (showId == 1 && tableDataModel.sitChairId > 0)
				{
					//ownCardIndex为-1时表示由玩家主动出牌
					if (ownCardIndex != -1)
					{
						showOwnDiscard(discard);
					}
				}
				else
				{
					showCard.x = showCardPosition.x;
					showCard.y = showCardPosition.y;
					showCard.visible = true;
				}

				//使用特效类
				var tweenObj:Object = {};
				if (targetCard)
				{
					var globalPoint:Point = targetCard.localToGlobal(new Point());
					tweenObj = {x: globalPoint.x, y: globalPoint.y, width: targetCard.width, height: targetCard.height};
				}
				else
				{
					tweenObj = {};
				}

				tweenObj.onComplete = function(disO:DisplayObject, position:int):void
				{
					disO.visible = false;
					if (tableDataModel != null && UIAllRefer.instance.getGameUIbyTableId( tableDataModel.tableDataFromLobby.tableID).visible == true)
					{
						//播放摸牌的声音
						SoundManager.instance.discardTileSoundPlay();
					}
					//抛出摸牌动画结束事件
					dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.DISCARD_EFFECT_FINISH));
				};
				tweenObj.onCompleteParams = [showCard, showId];

				setTimeout(function(param1:*, param2:*, param3:*):void
				{
					TweenLite.to(param1, param2, param3);
				}, 1000, showCard, duration, tweenObj);
			}
		}

		/**
		 * 显示自己要出的牌
		 * @param tile 牌的内容
		 * @param index 所在手牌的位置
		 */
		public function showOwnDiscard(tile:Object):void
		{
			var showId:int = 1;
			var duration:Number = 0.7;

			var data:Object = {position: 1, cardIndex: tableDataModel.handTilesNum + 1};
			dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.GET_OWN_DISCARD_INDEX, data));
			if (data.discardIndex)
			{
				data.cardIndex = data.discardIndex;
			}
			//获取牌
			dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.GET_DISCARD, data));
			//获取飞牌的起始位置
			dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.GET_FETCH_INFO, data));

			var card:MovieClip = data.card;
			var fetchToCard:MovieClip = data.fetchToCard;

			if (card && fetchToCard)
			{
				card.visible = true;
				card.width = fetchToCard.width;
				card.height = fetchToCard.height;
				var globalPoint:Point = fetchToCard.localToGlobal(new Point());
				card.x = globalPoint.x;
				card.y = globalPoint.y;
				var cardBitmap:Bitmap = card.getChildByName("cardBitmap") as Bitmap;
				if (cardBitmap)
				{
					cardBitmap.bitmapData = BigCardFactory.getNormalCardBitmapData(tile.type, tile.value);
					cardBitmap.visible = true;
				}
			}

			//使用特效类
			var tweenObj:Object = {x: UIAllRefer.stage.stageWidth / 2, y: 480};

			tweenObj.onComplete = function(disO:DisplayObject):void
			{

			};
			tweenObj.onCompleteParams = [card];

			TweenLite.to(card, duration, tweenObj);
		}

	}
}
